package com.itboye.hutouben.util;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.itboye.hutouben.R;
import com.itboye.hutouben.base.MyApplcation;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Timer;
import java.util.TimerTask;

public class BaseDialogFragment extends DialogFragment {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setStyle(STYLE_NO_TITLE, android.R.style.Theme_Translucent);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		smartInject(getView());
	}

	protected void smartInject(View view) {
		try {
			Class<? extends Fragment> clz = getClass();
			while (clz != BaseDialogFragment.class) {
				Field[] fs = clz.getDeclaredFields();
				Resources res = getResources();
				String packageName = getActivity().getPackageName();
				for (Field field : fs) {
					if (!View.class.isAssignableFrom(field.getType())) {
						continue;
					}
					int viewId = res.getIdentifier(field.getName(), "id",
							packageName);
					if (viewId == 0)
						continue;
					field.setAccessible(true);
					try {
						View v = view.findViewById(viewId);
						field.set(this, v);
						Class<?> c = field.getType();
						Method m = c.getMethod("setOnClickListener",
								View.OnClickListener.class);
						m.invoke(v, this);
					} catch (Throwable e) {
					}
					field.setAccessible(false);

				}
				clz = (Class<? extends Fragment>) clz.getSuperclass();

			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void showMyToast(final Toast toast, final int cnt) {
		final Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			@Override
			public void run() {
				toast.show();
			}
		}, 0, 3000);
		new Timer().schedule(new TimerTask() {
			@Override
			public void run() {
				toast.cancel();
				timer.cancel();
			}
		}, cnt);
	}

	public void toast(String text) {
		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(R.layout.toast, null);
		TextView textView = (TextView) view.findViewById(R.id.toast_tv);
		textView.setText(text);
		Toast toast = new Toast(MyApplcation.ctx);
		toast.setDuration(Toast.LENGTH_LONG);
		toast.setView(view);
		showMyToast(toast, 1000);
	}

}
